package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.Messages;
import com.serkol.ams.mapper.MessagesMapper;
import com.serkol.ams.service.MessagesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【messages】的数据库操作Service实现
 * @createDate 2023-10-25 18:16:51
 */
@Service
public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, Messages>
        implements MessagesService {

    @Override
    public Map<String, Map<String, String>> findByMaps() {
        Map<String, Map<String, String>> maps = new HashMap<>();
        Map<String, String> zhs = new HashMap<>();
        Map<String, String> ugs = new HashMap<>();
        List<Messages> list = this.list();
        Iterator<Messages> iterator = list.iterator();
        while (iterator.hasNext()) {
            Messages next = iterator.next();

            if (StringUtils.isNotEmpty(next.getZhUg())) {
                if(isChinese(next.getZhCn())) {
                    zhs.put(next.getZhCn(), next.getZhCn());
                    ugs.put(next.getZhCn(), next.getZhUg());
                }else{
                    zhs.put(next.getCode(), next.getZhCn());
                    ugs.put(next.getCode(), next.getZhUg());
                }
            }

        }
        maps.put("zh-CN", zhs);
        maps.put("zh-UG", ugs);
        return maps;
    }

    @Override
    public List<Messages> findByPage() {
        LambdaQueryWrapper<Messages> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Messages::getZhUg);
    return this.list(queryWrapper);
    }

    public boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}




